<?php
namespace app\admin\controller;

use app\common\model\Book as BookModel;
/**
 * 电子书控制器
 * Class Adminuser
 * @package app\index\controller
 */
class Book extends Base
{
    /**
     * [index  书本列表]
     * @return mixed|string
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $this->getPageAndSize($param);
        $model = new BookModel();
        $map = [];
        $user = $this->user;
        try{
            if (!empty($param['con']))
                $map['title|auther'] = ['like', "%" . $param['con'] . "%"];
            if(!empty($param['start']) && !empty($param['end']))
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            $map['status'] = ['neq',2];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/book/getIndex');
            return admin_json(-1,'获取失败'.$e->getMessage());
        }
        writelog('查看书本列表', 0,$user);
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postAdd  书本添加]
     * @return mixed|string
     * @author [C。]
     */
    public function postAdd()
    {
        $param = input('param.');
        $model = new BookModel();
        $user = $this->user;
        try{
            $id=$model->addData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/book/postAdd');
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        writelog('添加书本--'.$id, 0,$user);
        return admin_json(1,'添加成功');
    }
    /**
     * [postEdit  书本修改]
     * @return mixed|string
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new BookModel();
        $user = $this->user;
        try{
            $model->editData($param);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/book/postEdit');
            return admin_json(-1,'修改失败'.$e->getMessage());
        }
        writelog('修改书本--'.$param['id'], 1,$user);
        return admin_json(1,'修改成功');
    }
    /**
     * [getDel  书本删除]
     * @return mixed|string
     * @author [C。]
     */
    public function getDel()
    {
        $param = input('param.');
        $model = new BookModel();
        $user = $this->user;
        try{
            $model->editData(['status' => 2,'id' => $param['id']]);
        }catch (\Exception $e) {
            systembug($e->getMessage(),1,'admin/book/getDel');
            return admin_json(-1,'删除失败'.$e->getMessage());
        }
        writelog('删除书本--'.$param['id'], 1,$user);
        return admin_json(1,'删除成功');
    }
}